const app = getApp();
// const controllers = app.controllers;
const controllers=require("../../../controllers/ControllerVip").controller;

Page({
  data: {
    dataInfo:{},
    id:null,
    shopId:null
  },
  onLoad: function (options) {
    let id = options.id;
    let shopId = app.globalData.shopId;
    if(options.shopId){
      shopId = options.shopId;
    }
     if(options.scene){
        let scene=decodeURIComponent(options.scene);
        console.log(scene);
        let vipId=scene.split(",")[0].split(":")[1];
        let sId=scene.split(",")[1].split(":")[1];
        if(vipId){
          id = vipId;
        }
        if(sId){
          shopId = sId;
        }
     }
    if(shopId){
      wx.setStorageSync('shopId', shopId);
      app.globalData.shopId=shopId;
    }
    this.setData({
      'id':id,
      shopId:shopId
    });// || 28
    this.getInfo(id);
  },
  getInfo: function (id) {
    let d = {
      vipId:this.data.id,
      shopId:this.data.shopId
    }
    let that = this;
    controllers.getVipDetail2(d).then(res=>{
      that.setData({
        dataInfo:res.data
      })
    });
  },
  onOpenCard(){

      let shopId=this.data.shopId;
      let vipId=this.data.id;
      controllers.openCard({shopId:shopId,vipId:vipId}).then(res=>{
        if(res.code==1){
          let orderNo=res.data.orderNo;
          let openId=app.globalData.openId
          controllers.payMent({shopId:shopId,orderNo:orderNo,openId:openId,frontUrl:"mp:///pages/Index/Index"}).then(res=>{
              if(res.code==1){
                let data=JSON.parse(res.data);
                console.log(data)
                wx.requestPayment({
                  nonceStr: data.nonceStr,
                  package: data.package,
                  paySign: data.paySign,
                  timeStamp: data.timeStamp,
                  signType: data.signType,
                  success(res){
                    wx.showToast({
                      title: '购买成功',
                      duration:2000
                    });
                    console.info("##########",res);
                    wx.navigateTo({
                      url: '/pages/my/vip/vip_myCard/index'
                    })
                    

                  },
                  fail(res){
                    console.info(res);
                    if(res.errMsg&&res.errMsg.indexOf("cancel")==-1){
                      wx.showToast({
                        icon:"none",
                        title: '购买失败'+res.errMsg,
                        duration:2000
                      })
                    }
              
                  }
                });
              }
          })
        }else{
          wx.showToast({
            icon:"none",
            title: '开卡失败',
            duration:2000
          })
        }
      })
  },


  toUrl(e) {
    var d = e.currentTarget.dataset;
    let url = d.url;
    console.info(url);
    wx.navigateTo({
      url: url
    });
  },
  onBack(){
    wx.navigateBack();
  }

})